package system1

import (
	"strconv"

	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system1"
	system1Req "github.com/flipped-aurora/gin-vue-admin/server/model/system1/request"
	"gorm.io/gorm"
)

type WorkflowLogService struct {
}

// CreateWorkflowLog 创建审批历史记录
// Author [piexlmax](https://github.com/piexlmax)
func (WorkflowLogService *WorkflowLogService) CreateWorkflowLog(WorkflowLog *system1.WorkflowLog) (err error) {
	err = global.GVA_DB.Create(WorkflowLog).Error
	return err
}

// DeleteWorkflowLog 删除审批历史记录
// Author [piexlmax](https://github.com/piexlmax)
func (WorkflowLogService *WorkflowLogService) DeleteWorkflowLog(ID string, userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&system1.WorkflowLog{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
			return err
		}
		if err = tx.Delete(&system1.WorkflowLog{}, "id = ?", ID).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// DeleteWorkflowLogByIds 批量删除审批历史记录
// Author [piexlmax](https://github.com/piexlmax)
func (WorkflowLogService *WorkflowLogService) DeleteWorkflowLogByIds(IDs []string, deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&system1.WorkflowLog{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
			return err
		}
		if err := tx.Where("id in ?", IDs).Delete(&system1.WorkflowLog{}).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// UpdateWorkflowLog 更新审批历史记录
// Author [piexlmax](https://github.com/piexlmax)
func (WorkflowLogService *WorkflowLogService) UpdateWorkflowLog(WorkflowLog system1.WorkflowLog) (err error) {
	err = global.GVA_DB.Save(&WorkflowLog).Error
	return err
}

// UpdateWorkflowLog 更新审批历史记录
// Author [piexlmax](https://github.com/piexlmax)
func (WorkflowLogService *WorkflowLogService) AddWorkflowLog(uid uint, ID string, action string, comment string) (err error) {
	var WorkflowLog system1.WorkflowLog
	WorkflowLog.Action = action
	WorkflowLog.CreatedBy = uid
	id, _ := strconv.Atoi(ID)
	WorkflowLog.WorkflowDataId = uint(id)
	WorkflowLog.Comment = comment
	err = global.GVA_DB.Save(&WorkflowLog).Error
	return err
}

// GetWorkflowLog 根据ID获取审批历史记录
// Author [piexlmax](https://github.com/piexlmax)
func (WorkflowLogService *WorkflowLogService) GetWorkflowLog(ID string) (WorkflowLog system1.WorkflowLog, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&WorkflowLog).Error
	return
}

// GetWorkflowLogInfoList 分页获取审批历史记录
// Author [piexlmax](https://github.com/piexlmax)
func (WorkflowLogService *WorkflowLogService) GetWorkflowLogInfoList(info system1Req.WorkflowLogSearch) (list []system1.WorkflowLog, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&system1.WorkflowLog{})
	var WorkflowLogs []system1.WorkflowLog
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Order("created_at DESC,updated_at DESC").Find(&WorkflowLogs).Error
	return WorkflowLogs, total, err
}
